package nancal.mp

import integration.TestBase
import io.kubernetes.client.openapi.ApiClient
import jnr.unixsocket.UnixSocket
import jnr.unixsocket.UnixSocketAddress
import jnr.unixsocket.UnixSocketChannel
import nancal.mp.db.mongo.mor
import nancal.mp.service.K8sDeploymentService
import nancal.mp.service.K8sService
import nbcp.myoql.db.mongo.query
import org.junit.jupiter.api.Test
import org.springframework.beans.factory.annotation.Autowired
import java.io.BufferedReader
import java.io.File
import java.io.InputStreamReader
import java.io.PrintWriter


class k8s_test : TestBase() {
    @Autowired
    lateinit var k8sDeploy: K8sDeploymentService;

    @Autowired
    lateinit var k8sService: K8sService;


    @Autowired
    lateinit var k8sDeploymentService: K8sDeploymentService

    @Test
    fun testK8sRestart() {
        var ret = k8sDeploymentService.restart("", "default", "nginx")

        if (ret.msg.isNullOrEmpty()) {
            println("重启成功")
        } else {
            println(ret.msg)
        }
    }

    @Test
    fun a() {
        // 建立 Unix Socket 连接
        // 建立 Unix Socket 连接
        val sockFile = File("/var/run/docker.sock")
        val address = UnixSocketAddress(sockFile)
        val channel = UnixSocketChannel.open(address)
        UnixSocket(channel).use { unixSocket ->

            // 调用 Docker API

            // 调用 Docker API
            PrintWriter(unixSocket.getOutputStream()).use { w ->
                w.println("GET /v1.24/containers/json HTTP/1.1")
                w.println("Host: http")
                w.println("Accept: */*")
                w.println("")
            }
            // 关闭 Output，否则会导致下面的 read 操作一直阻塞
            // 关闭 Output，否则会导致下面的 read 操作一直阻塞
            unixSocket.shutdownOutput()

            // 获取返回结果

            // 获取返回结果
            println("---- Docker Response ----")
            val br = BufferedReader(InputStreamReader(unixSocket.getInputStream()))
            var line: String?
            while (br.readLine().also { line = it } != null) {
                println(line)
            }
        }
    }


    @Test
    fun testJenkinsLog() {
        var devPods = k8sDeploy.list("", "").data
        var uatPods = k8sDeploy.list("prepare", "").data

        uatPods.forEach { uat ->
            var dev = devPods.firstOrNull { it.name == uat.name }
            if (dev == null) {
                println("找不到dev名称：${uat.name}")
                return@forEach
            }

            if (uat.imageNameVersion != dev.imageNameVersion) {
                println("imageName 不匹配：${uat.name}, uat:${uat.imageNameVersion}, dev:${dev.imageNameVersion}")
                return@forEach
            }

            if (uat.imageNameVersion != dev.imageNameVersion) {
                println("imageVersion 不匹配：${uat.name}, uat:${uat.imageNameVersion}, dev:${dev.imageNameVersion}")
                return@forEach
            }
        }

    }

    private fun getUatK8s(): ApiClient {
        var deploySetting = mor.dev.appDeploySetting.query()
                .where { it.code mongoEquals "lecang-uat" }
                .toEntity()!!;

        var api = ApiClient();
        api.basePath = deploySetting.k8sConfig.apiServerUrl
        api.setApiKey(deploySetting.k8sConfig.apiToken);
        api.setApiKeyPrefix("Bearer")
        api.isVerifyingSsl = false;

        return api;
    }
}
